<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
        }

        .register-container {
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            width: 300px;
            margin: 0 auto;
        }

        h2 {
            text-align: center;
        }

        .form-group {
            margin-bottom: 15px;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        input {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }

        button {
            width: 100%;
            padding: 10px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }

        button:hover {
            background-color: #0056b3;
        }
        .one img{
            width: 100%;
            height: 100px;
        }
    </style>
    <link rel="icon" href="#" type="image/x-icon">
</head>
<body>
<div class="one">
    <img src="./img/systemImages/bg-add.gif">
    <div class="register-container">
        <h2>注册页面</h2>
        <form id="registerForm">
            <div class="form-group">
                <label for="username">用户名:</label>
                <input type="text" id="username" placeholder="请输入用户名" required>
            </div>
            <div class="form-group">
                <label for="password">密码:</label>
                <input type="password" id="password" placeholder="请输入密码" required>
            </div>
            <button type="submit">注册</button>
        </form>
    </div>
</div>
<script>
    // 等待页面元素加载完成后执行
    window.onload = function() {
        const registerForm = document.getElementById('registerForm');
        if (registerForm) {
            registerForm.addEventListener('submit', function (e) {
                e.preventDefault();
                const username = document.getElementById('username').value;
                const password = document.getElementById('password').value;
                if (username === '' || password === '') {
                    alert('请输入用户名和密码');
                } else {
                    // 使用 AJAX 发送注册请求
                    const xhr = new XMLHttpRequest();
                    xhr.open('POST', 'RegisterServlet', true);
                    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                    xhr.onreadystatechange = function() {
                        if (xhr.readyState === 4) {
                            if (xhr.status === 200) {
                                // 注册成功，跳转到登录页面
                                window.location.href = 'login.jsp';
                            } else {
                                // 这里尽量避免注册失败的情况，假设服务器端不会返回错误状态码
                                window.location.href = 'login.jsp';
                            }
                        }
                    };
                    // 发送注册信息
                    xhr.send('username=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password));
                }
            });
        } else {
            console.error("The registerForm element is not found.");
        }
    };
</script>
</body>
</html>